<?php
namespace backend\controllers;
use yii\web\Controller;
use backend\models\Admin;
use yii\helpers\Url;
use yii;
class PublicController extends Controller{

    //公共方法
    public function actions(){
        return [
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
                'width' => 90,
                'height' => 40,
                'padding' => 0,
                'minLength' => 4,
                'maxLength' => 4,
            ],
        ];
    }

    public function actionLogin(){
        Yii::$app->controller->layout = false;  //取消视图
        if(!\Yii::$app->user->isGuest){
            $this->redirect(Url::toRoute('public/hello'));
        }
        $model = new Admin(['scenario' => 'login']);
        if(Yii::$app->request->isPost){
           $data = Yii::$app->request->post();
           if($model->login($data)){
               return $this->redirect(Url::toRoute('public/hello'));
              // Yii::$app->end();
           }
        }
        return $this->render('login', ['model' => $model]);
    }

    public function actionHello(){
        $model = new Admin();
        return $this->render('hello', ['model' => $model]);
    }

    public function actionLogout(){
        Yii::$app->session->removeAll();
        if(!isset(Yii::$app->session['admin']['isLogin'])){
            $this->redirect(['public/login']);
            Yii::$app->end();
        }
        $this->goBack();
    }

    public function actionSeekpassword(){
        $this->layout = false;
        $model = new Admin(['scenario' => 'seekpass']);
        if(Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            if($model->seekpass($post)){
                Yii::$app->session->setFlash('info', '电子邮件已经发送成功,请查收');
            }
        }
        return $this->render('seekpassword', ['model' => $model]);
    }
}



















